package com.design.graduation.asdemo.controller;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.design.graduation.asdemo.R;
import com.design.graduation.asdemo.base.ActionBarActivity;
import com.design.graduation.asdemo.event.BusProvider;
import com.design.graduation.asdemo.event.PositionInfo;
import com.design.graduation.asdemo.manager.UserManager;
import com.design.graduation.asdemo.model.InfoEntity;
import com.design.graduation.asdemo.model.RoomMessage;
import com.design.graduation.asdemo.model.SolicitInfo;
import com.design.graduation.asdemo.model.SolicitingDetails;
import com.design.graduation.asdemo.network.HSFObserver;
import com.design.graduation.asdemo.network.model.RoomListResult;
import com.design.graduation.asdemo.utils.GeoHashKit;
import com.design.graduation.asdemo.utils.ResourceUtils;
import com.design.graduation.asdemo.utils.ToastUtil;
import com.design.graduation.asdemo.view.ListViewForScrollView;
import com.design.graduation.asdemo.view.adapter.ResultAdapter;
import com.squareup.otto.Subscribe;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lijinyuan on 16/2/18.
 */
@EActivity(R.layout.activity_soliciting)
public class SolicitingActivity extends ActionBarActivity implements SeekBar.OnSeekBarChangeListener {
    private SolicitInfo solicitInfo;
    private InfoEntity infoEntity;
    public int priceProgress;
    private int selectedIndexOfTab;
    private List<SolicitingDetails> mlist;
    private ResultAdapter resultAdapter;
    private Boolean showConditions = false;
    private String smallHash;
    private String bigHash;
    List<RoomMessage> mData;

    @ViewById(R.id.content)
    ListViewForScrollView listViewForScrollView;

    @ViewById(R.id.act_soliciting_price_seekbar)
    SeekBar mSeekBar;
    @ViewById(R.id.act_soliciting_price_number)
    TextView priceNumber;
    @ViewById(R.id.act_soliciting_position_et)
    TextView postitionTitle;
    @ViewById(R.id.act_soliciting_conditions_more)
    RecyclerView conditionsMore;
    @ViewById(R.id.fab)
    FloatingActionButton fab;
    @ViewById(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BusProvider.register(this);
        solicitInfo = new SolicitInfo();
        selectedIndexOfTab = 0;
    }


    @AfterViews
    public void initView() {
        mData = new ArrayList<>();
        infoEntity = new InfoEntity();
        mlist = new ArrayList<>();
        mSeekBar.setOnSeekBarChangeListener(this);
        setToolBar();


    }

    @Click(R.id.act_soliciting_position_re)
    public void toLocation() {
        startActivity(new Intent(SolicitingActivity.this, LocationActivity.class));
    }

    @Subscribe
    public void getPosition(PositionInfo position) {
        postitionTitle.setText(position.getEntity().getAddress().toString());
        solicitInfo.setLat(position.getEntity().getLat());
        solicitInfo.setLon(position.getEntity().getLon());
    }


    public void toSearch(View view) {
        solicitInfo.setPrice(priceProgress + "");
        if (solicitInfo.getLat() == 0) {
            ToastUtil.showShortToast("请选择位置");
        } else if (solicitInfo.getPrice() == null) {
            ToastUtil.showShortToast("请输入价格");
        } else {
            GeoHashKit hashKit = new GeoHashKit();
            String hash = hashKit.encode(solicitInfo.getLat(), solicitInfo.getLon());
            smallHash = hash.substring(0, 5);
            UserManager.getRoomList(smallHash, new HSFObserver<RoomListResult>() {
                @Override
                public void onSuccess(RoomListResult roomListResult) {
                    mData = roomListResult.getData();
//                    LogUtils.d(JSONUtil.toJSONString(roomListResult.getData()));
                }
            });

            if (mData != null) {
                for (int i = 0; i < mData.size(); i++) {
                    mlist.clear();
                    mlist.get(i).setPrice(mData.get(i).getPrice());
                    mlist.get(i).setContent(mData.get(i).getContent());
                    mlist.get(i).setPosition(mData.get(i).getPosition());
                    List<String> urlList=JSON.parseArray(mData.get(i).getImgurl(), String.class);
                    
                    mlist.get(i).setImageUrl(mData.get(i).getImgurl());

                }
                resultAdapter = new ResultAdapter(this, mlist, R.layout.item_details);
                listViewForScrollView.setAdapter(resultAdapter);
            }
        }
    }

    //底部筛选
    public void onTabSelected(View view) {
        ViewGroup viewGroup = (ViewGroup) view.getParent();
        int indexOfTab = viewGroup.indexOfChild(view);
        changeFragmentByIndexOfTab(indexOfTab);

    }

    private void changeFragmentByIndexOfTab(int indexOfTab) {
        if (indexOfTab == selectedIndexOfTab) return;
        changeTabState(indexOfTab);
        selectedIndexOfTab = indexOfTab;

    }

    private void changeTabState(int indexOfTab) {
        LinearLayout tabLayout = (LinearLayout) this.findViewById(R.id.tab_layout);
        LinearLayout newSelected = (LinearLayout) tabLayout.getChildAt(indexOfTab);
        LinearLayout selectedTab = (LinearLayout) (tabLayout.getChildAt(selectedIndexOfTab));
        selectedTab.setBackgroundColor(ResourceUtils.getColor(this, R.color.white));
        newSelected.setBackgroundColor(ResourceUtils.getColor(this, R.color.black));
    }


    @AfterViews
    public void initViews() {
        mlist = new ArrayList<>();
        mlist.add(new SolicitingDetails("111111", "aaaaa", "11", "http://image.tianjimedia.com/uploadImages/2013/256/P8EF9H125FCN_1000x500.jpg"));
        mlist.add(new SolicitingDetails("222222", "bbbbb", "22", "http://image.tianjimedia.com/uploadImages/2013/256/P8EF9H125FCN_1000x500.jpg"));
        mlist.add(new SolicitingDetails("333333", "ccccc", "33", "http://image.tianjimedia.com/uploadImages/2013/256/P8EF9H125FCN_1000x500.jpg"));
        mlist.add(new SolicitingDetails("444444", "ddddd", "44", "http://image.tianjimedia.com/uploadImages/2013/256/P8EF9H125FCN_1000x500.jpg"));

        resultAdapter = new ResultAdapter(this, mlist, R.layout.item_details);
        listViewForScrollView.setAdapter(resultAdapter);

        listViewForScrollView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(SolicitingActivity.this, CheckDetailsActivity.class));
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        BusProvider.unregister(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        priceProgress = progress;
        priceNumber.setText("0-" + progress + "元");
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem item = menu.findItem(R.id.action_text);
        MenuItem item1 = menu.findItem(R.id.action_img);
        item.setVisible(false);
        item1.setVisible(false);
        return true;
    }

    private void setToolBar() {
        toolbar.setTitle("设置筛选条件");
        setSupportActionBar(toolbar);
//        toolbar.setNavigationIcon(R.drawable.icon_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_img:

                        finish();
                        break;
                }
                return true;
            }
        });

    }
}

